package com.xin.servie.impl;

import com.xin.exception.MyException;
import com.xin.servie.FileService;
import com.xin.utils.FastdfsUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.jsf.FacesContextUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 文件上传
 */
@Service
public class FileServiceImpl implements FileService{

    @Value("${file.fileUrl}")
    private String serverUrl;
    @Override
    public String fileUpload(MultipartFile file) {

        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i+1);
        try {
            byte[] bytes = file.getBytes();
            String upload = FastdfsUtil.upload(bytes, substring);
            return upload;
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyException("服务器繁忙，请稍后在上传文件");
        }
    }

    /**
     * 删除文件
     * @param filePath
     * @return
     */
    @Override
    public boolean deleteFile(String filePath) {
        //删除文件列表
        boolean delete = FastdfsUtil.delete(filePath);
        return delete;
    }
}
